//
//  TestVC.m
//  XTeng
//
//  Created by 周成龙 on 16/8/12.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "TestVC.h"

#import "VDJConstant.h"
#import "NoticeHeader.h"
#import "Header.h"

#import "TestCell.h"
#import "TestButton.h"

@interface TestVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *contentArray;

@end

@implementation TestVC
#pragma mark - getter
- (UITableView *)tableView
{
    if (nil == _tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = kClearClolor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    self.titleLabel.text = @"考试倒计时";
    self.contentArray = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6"];
    
    [self initLeftButton];
    [self initRightButton];
    [self initMenuView];
    
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout.spaceToSuperView(UIEdgeInsetsMake([self getStartOriginY] + 44, 0, 49, 0));
}
#pragma mark - left button
- (void)initLeftButton
{
    UIButton *left = [[UIButton alloc] init];
    left.bounds = CGRectMake(0, 0, 60, 20);
    left.center = CGPointMake(kMargin + 30, self.navBarView.center.y + 10);
    [left setTitle:@"课表" forState:UIControlStateNormal];
    left.titleLabel.font = kFontWithSize(14);
    [left setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [left addTarget:self action:@selector(leftuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:left];
}

- (void)leftuttonClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeTabBar object:@{@"value":@"isNotTest"}];
}
#pragma mark - right button
- (void)initRightButton
{
    UIButton *right = [[UIButton alloc] init];
    right.bounds = CGRectMake(0, 0, 60, 30);
    right.center = CGPointMake(kScreenWidth - (kMargin + 20), self.navBarView.center.y + 10);
    [right setImage:[UIImage imageNamed:@"+"] forState:UIControlStateNormal];
    right.titleLabel.font = kFontWithSize(14);
    [right setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [right addTarget:self action:@selector(rightbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:right];
}

- (void)rightbuttonClick:(UIButton *)sender
{
    MyLog(@"rightbuttonClick");
}

#pragma mark - menu view
- (void)initMenuView
{
    UIView *menuView = [[UIView alloc] init];
    menuView.frame = CGRectMake(0, [self getStartOriginY], kScreenWidth, 44);
    menuView.backgroundColor = kWhiteColor;
    [self.view addSubview:menuView];
    
    if (_segmentedControl == nil) {
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"备考中(3)", @"已结束(3)"]];
        _segmentedControl.frame = CGRectMake(kMargin, 5, kScreenWidth - 2*kMargin, 34);
        _segmentedControl.selectedSegmentIndex = 0;
        _segmentedControl.tintColor = kNavBackgroundColor;
        [_segmentedControl addTarget:self action:@selector(segmentedClickItem:) forControlEvents:UIControlEventValueChanged];
        [menuView addSubview:_segmentedControl];
    }
}

- (void)segmentedClickItem:(UISegmentedControl *)segmented
{
    MyLog(@"-->%ld", segmented.selectedSegmentIndex);
    if (segmented.selectedSegmentIndex == 0) {
        self.contentArray = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6"];
    }else {
        self.contentArray = @[@"5", @"5", @"5", @"5"];
    }
    [self.tableView reloadData];
}

#pragma mark - table view datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.contentArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (nil == cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:nil options:nil] firstObject];
    }
    
    NSString *str = self.contentArray[indexPath.section];
    cell.row = [str integerValue];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001f;
}

@end
